<template>
	<section>
		<!--工具条-->
		<el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
			<el-form :inline="true" :model="filters">
				<el-form-item>
					<el-button type="primary" @click="handleAdd">新增</el-button>
				</el-form-item>
			</el-form>
		</el-col>

		<!--列表-->
		<el-table
				:data="list.filter(data => !search || data.name.toLowerCase().includes(search.toLowerCase()))"
				border
				row-key="_id"
				:tree-props="{children: 'children', hasChildren: 'hasChildren'}"
				 style="width: 100%;">
			<el-table-column prop="name" label="名称" min-width="120" sortable></el-table-column>
			<el-table-column label="图片" width="120">
				<template slot-scope="scope">
					<img  style="width: 100px" :src="scope.row.imgList[0].raw.src" alt="">
				</template>
			</el-table-column>
			<el-table-column prop="price" label="价格" min-width="120" sortable></el-table-column>
			<el-table-column prop="memberPrice" label="会员价格" min-width="120" sortable></el-table-column>
			<el-table-column prop="trademark" label="品牌" min-width="120" sortable></el-table-column>
			<el-table-column prop="serialNumber" label="编号" min-width="120" sortable></el-table-column>
			<el-table-column prop="serviceTel" label="客服电话" min-width="120" sortable></el-table-column>
			<el-table-column label="操作" width="250">
				<template slot="header" slot-scope="scope">
					<el-input
							v-model="search"
							size="mini"
							placeholder="输入名称搜索"/>
				</template>
				<template slot-scope="scope">
					<el-button size="small" @click="editSubmit(scope.$index, scope.row)">编辑</el-button>
					<el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
				</template>
			</el-table-column>
		</el-table>

	</section>
</template>

<script>
	import {getGoodsList,delGoods} from "@/api/goods/api";
	export default {
		data() {
			return {
				filters: {
					name: ''
				},
				list: [],
				search: ''
			}
		},
		methods: {
			//获取商品列表
			getGoodsList() {
				getGoodsList().then((res) => {
					this.list = res.data;
				});
			},
			//删除
			handleDel(index, row) {
				this.$confirm('确认删除该记录吗?', '提示', {
					type: 'warning'
				}).then(() => {
					let para = { id: row._id };
					delGoods(para).then((res) => {
						this.getGoodsList();
					});
				}).catch(() => {

				});
			},
			//显示新增界面
			handleAdd() {
				this.$router.push({path:'commodityDetail'})
			},
			//编辑
			editSubmit(index, row) {
				this.$router.push({path:'commodityDetail',query: { id: row._id  }})
			},
		},
		mounted() {
			this.getGoodsList();
		}
	}

</script>

<style lang="less">
   .viewNews img{
      width: 100%;
		 height: auto;
    }
</style>
